🔥 feat(nextjs): Bound Optimization components & Isomorphic Optimization Provider#355
Merged
Charles Hudson (phobetron) merged 1 commit intoJul 6, 2026
Conversation
1575048 to
747554c
Compare
c09fe50 to
41dc1a7
Compare
Wiz Scan Summary
To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension. |
3a811d6 to
5e13147
Compare
bfe1579 to
483d0af
Compare
Introduce router-specific bound Next.js SDK integration surfaces for App Router and Pages Router. Apps bind Optimization SDK config once in an app-local module, then use returned roots, providers, optimized entries, route trackers, proxies, and server helpers across server/client rendering boundaries. Original to this branch: - Add the App Router, Pages Router, and Pages Router server entrypoints for bound Next.js integrations while keeping /client, /server, /esr, /request-handler, /tracking-attributes, and /api-schemas as lower-level escape hatches. - Add automatic App Router server/client components that read request consent, cookies, headers, server Optimization data, entry variants, merge tags, tracking attributes, and state handoff through the bound proxy/root path. - Add the Pages Router bound client factory and config-bound getServerSideOptimizationProps() helper for getServerSideProps state handoff, response cookie persistence, client consent defaults, and initial page-event deduplication. - Rename and rebuild the Next.js reference implementations around nextjs-sdk_app-router and nextjs-sdk_pages-router, replacing the older SSR/Hybrid examples. - Update the reference apps to use app-local bound components, shared EntryCardContent rendering, merge-tag-aware rich text, live updates, preview panel wiring, and route tracking. - Refresh Next.js, React Web, Web SDK, E2E, consent, locale, profile synchronization, interaction tracking, analytics forwarding, and SDK selection docs around the App Router and Pages Router paths. - Extend React Web OptimizedEntry render props with OptimizedEntryRenderContext/getMergeTagValue and expose runtime/presentation helpers needed by SSR handoff and tracking. - Update package exports, build output declarations, bundle-size budgets, CI path filters, root scripts, unit tests, and browser E2E coverage for the new router names and server-resolved variant checks. Adopted from Tim's `feat/ssr-isomorphic-provider` work (#354): - Add the isomorphic OptimizationRuntime/SnapshotRuntime and runtime subpaths for core/web packages. - Make OptimizationProvider/OptimizationRoot render during SSR from serverOptimizationState, then hydrate into the live browser SDK. - Replace the old NextjsOptimizationState handoff path with provider/root serverOptimizationState handoff. - Fix SSR variant rendering by priming snapshot/current optimization state before client effects run. - Preserve injected-SDK first paint, align SnapshotRuntime consent gating with the live SDK, and keep SSR page/variant behavior working without browser JS. BREAKING CHANGE: NextjsOptimizationState is removed from the client entrypoint. Pass server optimization data through OptimizationRoot or OptimizationProvider via serverOptimizationState, use createNextjsAppRouterOptimization() for App Router handoff, or use createNextjsPagesRouterOptimization() with getServerSideOptimizationProps() for Pages Router handoff. [[NT-3560](https://contentful.atlassian.net/browse/NT-3560)]
483d0af to
718b6d9
Compare
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces router-specific bound Next.js SDK integration APIs for
@contentful/optimization-nextjs:createNextjsAppRouterOptimization()from@contentful/optimization-nextjs/app-routercreateNextjsPagesRouterOptimization()from@contentful/optimization-nextjs/pages-routercreateNextjsPagesRouterOptimization()from@contentful/optimization-nextjs/pages-router/serverConsumers can now bind Optimization SDK configuration once in an app-local module and use the returned
OptimizationRoot,OptimizationProvider,OptimizedEntry, route trackers, App Routerproxy, or Pages RoutergetServerSideOptimizationProps()across server and client rendering boundaries.The change also brings in the isomorphic SSR provider/runtime foundation needed for those APIs: React Web can render personalized content during SSR from snapshot state, then hydrate into the live browser SDK without app code branching on
typeof window.It also extends the React Web
OptimizedEntryrender-prop contract withOptimizedEntryRenderContextandgetMergeTagValue, exposes runtime/presentation helpers used by SSR handoff and tracking, replaces the older Next.js SSR/Hybrid reference apps with App Router and Pages Router implementations, and refreshes docs, package exports, CI, scripts, unit tests, and browser E2E coverage around the new router-specific integration path.Credit
Credit to Tim's
feat/ssr-isomorphic-providerbranch and PR #354 for the isomorphic SSR foundation adopted here, includingOptimizationRuntime,SnapshotRuntime, runtime package subpaths, server-renderableOptimizationProvider,serverOptimizationStatefirst-paint handoff, SSR variant-rendering fixes, injected-SDK first-paint fixes, and JavaScript-disabled SSR validation.Example usage
App Router
Pages Router
Architectural and design changes
/app-router,/pages-router, and/pages-router/server./app-routerresolves to the automatic server implementation in React Server Component contexts and the client implementation in browser/client contexts./clientas router-neutral React Web client surfaces./server,/esr,/request-handler,/tracking-attributes, and/api-schemasas lower-level escape hatches for custom flows.serverOptimizationStatehandoff.getServerSideProps, calls the request-bound SDK, writes or clearsctfl-opt-aid, returns serializableprops.contentfulOptimization, and avoids duplicate initial page events when the server already emitted one.NextjsOptimizationStatein favor ofserverOptimizationStatehandoff through root/provider components, automatic App Router handoff, or Pages RouterpagePropshandoff.OptimizedEntryrender props from(resolvedEntry)to(resolvedEntry, context), wherecontext.getMergeTagValueis available to rich text renderers.Isomorphic SSR foundation
OptimizationRuntime,SnapshotRuntime,createSnapshotRuntime, and runtime package subpaths for core/web.OptimizationProvider/OptimizationRootrender during SSR fromserverOptimizationState.useOptimization()andOptimizedEntryto resolve personalized content during SSR from snapshot state, then switch to the live SDK after hydration.serverOptimizationStateis provided.SnapshotRuntimeconsent gating with the live SDK.Benefits to consumers
OptimizedEntryrender-prop shape across server and browser rendering.Benefits to maintainers
Reference implementation rendering improvements
nextjs-sdk_ssrandnextjs-sdk_hybridwithnextjs-sdk_pages-routerandnextjs-sdk_app-router.createNextjsAppRouterOptimization()module, exportedproxy, layout-levelOptimizationRoot, andNextAppAutoPageTracker.createNextjsPagesRouterOptimization()module, a server-onlygetServerSideOptimizationProps()helper, and_app.tsxstate handoff.EntryCardContentrendering so server and client entry cards use the same rich text, nested-entry, click-target, and test-attribute behavior.getMergeTagValuefromOptimizedEntryrender props into rich text render options, aligning merge-tag rendering with the resolved entry/profile context.Breaking change
NextjsOptimizationStateis removed from the client entrypoint. Pass server optimization data throughOptimizationRootorOptimizationProviderviaserverOptimizationState, usecreateNextjsAppRouterOptimization()to let the App Router bound root/provider handle server-to-browser state handoff automatically, or usecreateNextjsPagesRouterOptimization()withgetServerSideOptimizationProps()to pass Pages Router state throughpageProps.[NT-3560]